//
//  DetailView.swift
//  willdo
//
//  Created by zz on 3/16/18.
//  Copyright © 2018 will do. All rights reserved.
//

import UIKit

class DetailView: UIView {

    let scrollView = UIScrollView()
    
    let TopView = UIView()
    let TaskName = UILabel()
    let EmergentState = UILabel()
    let MarkStar = UIImageView()
    let LeaderLabel = UILabel()
    let Leader = UIButton()
    let DeadLine = UILabel()
    let TaskState = UILabel()
    
    let BottomView = UIView()
    let AvatarImage = UIButton()
    let LeaderSpeak = UIButton()
    let StartTime = UILabel()
    let AllFile = UIButton()
    let HistoryMsg = UIButton()
    let TaskMsg = UILabel()
    
    let cellView = UIView()
    
    private var tmpView:UIView!
    
    let ChangeStateBtn = UIButton()
    
    let replaymsg = UITextField()
    
    var delegate:DetailViewDelegate?
    private var modle:MyTaskDetailModle!
    init(frame: CGRect, modle:MyTaskDetailModle){
        super.init(frame: frame)
        tmpView = TaskMsg
        self.modle = modle
        
        addSubview(scrollView)
        scrollView.contentSize = CGSize(width: frame.width, height: frame.height)
        scrollView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }

        scrollView.addSubview(TopView)
        scrollView.addSubview(BottomView)
        setupTopView()
        TopView.backgroundColor = UIColor.white
        TopView.snp.makeConstraints { (make) in
            make.top.left.equalToSuperview().offset(10)
            make.right.equalTo(self).offset(-10)
            make.height.equalToSuperview().dividedBy(6)
        }
        
        setupBottomView()
        BottomView.backgroundColor = UIColor.white
//        BottomView.snp.makeConstraints { (make) in
//            make.top.equalTo(TopView.snp.bottom).offset(10)
//            make.left.width.equalTo(TopView)
//            make.bottom.equalTo(TaskMsg.snp.bottom).offset(5)
//        }
        
        if (modle.file != nil){
            setupFileView(files: modle.file!)
        }
        if (modle.taskModle.state != .needReceive){
            setupReplayView()
        }
        BottomView.snp.makeConstraints { (make) in
            make.top.equalTo(TopView.snp.bottom).offset(10)
            make.left.width.equalTo(TopView)
            make.bottom.equalTo(tmpView.snp.bottom).offset(5)
        }
        BottomView.layoutIfNeeded()
        tmpView = BottomView
        
        if (modle.taskModle.state != .needReceive){
            setupCellView()
        }
        
        setupChangeBtn()
        
    }

    
    
    func setupTopView(){
        TopView.addSubview(TaskName)
        TopView.addSubview(EmergentState)
        TopView.addSubview(LeaderLabel)
        TopView.addSubview(Leader)
        TopView.addSubview(DeadLine)
        TopView.addSubview(TaskState)
        
        TaskName.text = modle.taskModle.name
        TaskName.font = UIFont(name: "PingFang SC", size: 17)
        TaskName.textColor = UIColor(hexString:"#040000")
        
        EmergentState.layer.cornerRadius = 6
        EmergentState.clipsToBounds = true
        EmergentState.text = modle.emergencyState.rawValue
        EmergentState.font = UIFont(name: "PingFang SC", size: 12)
        EmergentState.backgroundColor = UIColor(hexString:"#d14d4e")
        EmergentState.textAlignment = .center
        EmergentState.textColor = UIColor.white
        
        LeaderLabel.text = "指派人: "
        LeaderLabel.font = UIFont(name: "PingFang SC", size: 15)
        LeaderLabel.textColor = TaskName.textColor
        
        let attributeTitle = NSAttributedString(string: modle.taskModle.member, attributes:
            [NSAttributedStringKey.font : UIFont(name: "PingFang SC", size: 15)!,
             NSAttributedStringKey.foregroundColor:LeaderLabel.textColor])
        Leader.setAttributedTitle(attributeTitle, for: .normal)
        
        DeadLine.text = "截止日期: "+modle.taskModle.time
        DeadLine.font = LeaderLabel.font
        DeadLine.textColor = LeaderLabel.textColor
        
        TaskState.text = modle.taskModle.state.rawValue
        TaskState.font = DeadLine.font
        var fontColor = "#000000"
        switch modle.taskModle.state {
        case .needReceive: fontColor = "#2683f5"
        case .needCommit: fontColor = "#d24d4e"
        case .needReview: fontColor = "#f8b551"
        case .needArchive: fontColor = "#61b87f"
        }
        TaskState.textColor = UIColor(hexString:fontColor)

        
        let redImg0 = UIImageView(image: UIImage(named:"sign_picture"))
        TopView.addSubview(redImg0)
        redImg0.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(14)
            make.left.equalToSuperview().offset(13)
            make.width.height.equalTo(13)
        }
        TaskName.snp.makeConstraints { (make) in
            make.centerY.equalTo(redImg0)
            make.left.equalTo(redImg0.snp.right).offset(13)
            make.height.equalTo(15)
        }
        EmergentState.snp.makeConstraints { (make) in
            make.left.equalTo(TaskName.snp.right).offset(13)
            make.centerY.equalTo(TaskName.snp.centerY)
            make.height.equalTo(18)
            make.width.equalTo(36)
        }
        let leaderImg = UIImageView(image: UIImage(named:"figure icon"))
        TopView.addSubview(leaderImg)
        leaderImg.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(22)
            make.top.equalTo(TaskName.snp.bottom).offset(21)
            make.height.width.equalTo(14)
        }
        LeaderLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(leaderImg)
            make.left.equalTo(leaderImg.snp.right).offset(10)
            make.height.equalTo(24)
        }
        Leader.snp.makeConstraints { (make) in
            make.centerY.equalTo(LeaderLabel)
            make.left.equalTo(LeaderLabel.snp.right).offset(2)
            make.height.equalTo(24)
        }
        let DeadImg = UIImageView(image: UIImage(named:"date icon"))
        TopView.addSubview(DeadImg)
        DeadImg.snp.makeConstraints { (make) in
            make.left.equalTo(leaderImg)
            make.top.equalTo(leaderImg.snp.bottom).offset(22)
            make.height.width.equalTo(leaderImg)
        }
        DeadLine.snp.makeConstraints { (make) in
            make.left.equalTo(LeaderLabel.snp.left)
            make.centerY.equalTo(DeadImg)
            make.height.equalTo(24)
        }
        TaskState.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-11)
            make.bottom.equalTo(TopView.snp.bottom).offset(-12)
            make.height.equalTo(23)
        }
        
    }
    
    func setupBottomView(){
        BottomView.addSubview(AvatarImage)
        BottomView.addSubview(LeaderSpeak)
        BottomView.addSubview(StartTime)
        BottomView.addSubview(AllFile)
        BottomView.addSubview(HistoryMsg)
        
        BottomView.addSubview(TaskMsg)
        
        AvatarImage.setImage(UIImage(named:"head portrait"), for: .normal)
        AvatarImage.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(11)
            make.left.equalToSuperview().offset(11)
            make.width.height.equalTo(29)
        }
        
        let attributeTitle = NSAttributedString(string: modle.name, attributes:
            [NSAttributedStringKey.font : UIFont(name: "PingFang SC", size: 15)!,
             NSAttributedStringKey.foregroundColor:UIColor(hexString:"#040000")])
        LeaderSpeak.setAttributedTitle(attributeTitle, for: .normal)
        LeaderSpeak.sizeToFit()
        LeaderSpeak.snp.makeConstraints { (make) in
            make.top.equalTo(AvatarImage).offset(-2)
            make.left.equalTo(AvatarImage.snp.right).offset(8)
            make.height.equalTo(22)
        }
        
        StartTime.text = modle.commitTime
        StartTime.font = UIFont.init(name: "PingFang SC", size: 9)
        StartTime.textColor = UIColor(hexString: "#929292")
        StartTime.snp.makeConstraints { (make) in
            make.bottom.equalTo(AvatarImage).offset(2)
            make.left.equalTo(LeaderSpeak).offset(3)
        }
        
        HistoryMsg.setImage(UIImage(named:"messege"), for: .normal)
        HistoryMsg.addTarget(self, action: #selector(Communicat), for: .touchUpInside)
        HistoryMsg.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-15)
            make.top.equalTo(AvatarImage)
            make.width.height.equalTo(24)
        }
        
        AllFile.setImage(UIImage(named:"picture"), for: .normal)
        AllFile.addTarget(self, action: #selector(taskFile), for: .touchUpInside)
        AllFile.snp.makeConstraints { (make) in
            make.right.equalTo(HistoryMsg.snp.left).offset(-14)
            make.top.equalTo(HistoryMsg)
            make.height.width.equalTo(HistoryMsg)
        }
        
        TaskMsg.text = modle.commitMsg
        TaskMsg.numberOfLines = 0
        TaskMsg.textAlignment = .justified
        TaskMsg.textColor = UIColor(hexString:"040000")
        TaskMsg.font = UIFont(name: "PingFang SC", size: 13)
        
        TaskMsg.sizeToFit()
        TaskMsg.snp.makeConstraints { (make) in
            make.top.equalTo(AvatarImage.snp.bottom).offset(11)
            make.left.equalToSuperview().offset(14)
            make.right.equalToSuperview().offset(-14)
        }
        
    }
    
    func setupFileView(files:[MyTaskFile]){
        //BottomView.addSubview(fileViews)
        
        for file in files{
            let fileView = FileView(file: file)
            BottomView.addSubview(fileView)
            fileView.snp.makeConstraints({ (make) in
                make.top.equalTo(tmpView.snp.bottom).offset(4)
                make.left.right.equalTo(tmpView)
                make.height.equalTo(80)
            })
            tmpView = fileView
        }
    }
    
    func setupReplayView(){
        BottomView.addSubview(replaymsg)
        replaymsg.placeholder = "回复..."
        replaymsg.font = UIFont(name: "PingFang SC", size: 13)
        replaymsg.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 6, height: 0))
        replaymsg.leftViewMode = .always
        replaymsg.layer.borderWidth = 1
        replaymsg.layer.cornerRadius = 5
        replaymsg.layer.borderColor = UIColor(hexString:"#c2c2c2").cgColor
        replaymsg.snp.makeConstraints { (make) in
            make.left.right.equalTo(TaskMsg)
            make.top.equalTo(tmpView.snp.bottom).offset(11)
            make.height.equalTo(26)
        }
        tmpView = replaymsg
    }
    
    func setupCellView(){
        addSubview(cellView)
        cellView.backgroundColor = UIColor.white
        cellView.layer.cornerRadius = 5
        cellView.snp.makeConstraints { (make) in
            make.left.right.equalTo(BottomView)
            make.height.equalTo(75)
            make.top.equalTo(BottomView.snp.bottom).offset(10)
        }
        tmpView = cellView
        
        let subTaskView = CellView(title: "子任务", onView: self, onClick: #selector(SubTask))
        let taskMemberView = CellView(title: "任务成员", onView: self, onClick: #selector(taskMember))
        let sepLine = UIView()
        sepLine.backgroundColor = UIColor(hexString:"#f0eff5")
        cellView.addSubview(sepLine)
        
        cellView.addSubview(subTaskView)
        cellView.addSubview(taskMemberView)
        subTaskView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalToSuperview()
            make.height.equalToSuperview().dividedBy(2)
        }
        sepLine.snp.makeConstraints { (make) in
            make.right.equalToSuperview()
            make.height.equalTo(1)
            make.top.equalTo(subTaskView.snp.bottom)
            make.left.equalToSuperview().offset(12)
        }
        taskMemberView.snp.makeConstraints { (make) in
            make.left.right.equalTo(subTaskView)
            make.top.equalTo(subTaskView.snp.bottom)
            make.height.equalTo(subTaskView)
        }
        
    }
    
    @objc func Communicat(){
        delegate?.communicaBtnClick()
    }
    
    @objc func SubTask(){
        delegate?.subTask()
    }
    
    @objc func taskMember(){
        delegate?.taskMember()
    }
    
    @objc func taskFile(){
        delegate?.taskFile()
    }
    
    func setupChangeBtn(){
        
        scrollView.addSubview(ChangeStateBtn)
        var btnTitle:String = "接受任务"
        switch modle.taskModle.state {
        case .needReceive:
            btnTitle = "接受任务"
        case .needCommit:
            btnTitle = "提交任务"
        case .needReview:
            btnTitle = "撤销提交"
        case .needArchive:
            btnTitle = "归档任务"
        }
        let attributeTitle = NSAttributedString(string: btnTitle, attributes:
            [NSAttributedStringKey.font : UIFont(name: "PingFang SC", size: 17)!,
             NSAttributedStringKey.foregroundColor:UIColor(hexString:"#d14d4e")])
        ChangeStateBtn.backgroundColor = UIColor.white
        ChangeStateBtn.setAttributedTitle(attributeTitle, for: .normal)
        ChangeStateBtn.titleLabel?.textAlignment = .center
        ChangeStateBtn.layer.cornerRadius = 5
        ChangeStateBtn.snp.makeConstraints { (make) in
            make.top.equalTo(tmpView.snp.bottom).offset(15)
            make.centerX.equalToSuperview()
            make.width.equalToSuperview().offset(-20)
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class FileView: UIView {
    let shortImg = UIImageView()
    let fileTitle = UILabel()
    let fileSize = UILabel()
    let fileDate = UILabel()
    let fileMember = UILabel()
    let downLoadBtn = UIButton()
    init(file:MyTaskFile) {
        super.init(frame: CGRect.zero)
        self.layer.borderWidth = 1
        self.layer.cornerRadius = 5
        self.layer.borderColor = UIColor(hexString:"#929292").cgColor
        addSubview(shortImg)
        addSubview(fileTitle)
        addSubview(fileSize)
        addSubview(fileDate)
        addSubview(fileMember)
        addSubview(downLoadBtn)

        fileTitle.text = file.name
        fileTitle.font = UIFont(name: "PingFang SC", size: 13)
        fileTitle.textColor = UIColor(hexString:"#040000")
        fileSize.text = file.size
        fileSize.font = UIFont(name: "PingFang SC", size: 10)
        fileSize.textColor = UIColor(hexString:"#929292")
        
        fileDate.text = file.time
        fileDate.font = fileSize.font
        fileDate.textColor = fileSize.textColor
        fileMember.text = "来自 " + file.member
        fileMember.font = fileDate.font
        fileMember.textColor = fileDate.textColor
        
        shortImg.image = UIImage(named: "BIG_picture")
        shortImg.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(10)
            make.height.equalTo(60)
            make.width.equalTo(80)
        }
        fileTitle.snp.makeConstraints { (make) in
            make.top.equalTo(shortImg).offset(4)
            make.left.equalTo(shortImg.snp.right).offset(10)
        }
        fileSize.snp.makeConstraints { (make) in
            make.left.equalTo(fileTitle)
            make.centerY.equalToSuperview().offset(2)
        }
        fileDate.snp.makeConstraints { (make) in
            make.centerY.equalTo(fileSize)
            make.left.equalTo(fileSize.snp.right).offset(4)
        }
        fileMember.snp.makeConstraints { (make) in
            make.bottom.equalTo(shortImg).offset(-4)
            make.left.equalTo(fileTitle)
        }
        
        downLoadBtn.setImage(UIImage(named:"download"), for: .normal)
        downLoadBtn.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-16)
            make.width.height.equalTo(24)
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class CellView: UIView {
    let titleLabel = UILabel()
    let arrowImg = UIImageView()
    let clearBtn = UIButton()
    init(title:String, onView:Any, onClick:Selector) {
        super.init(frame: CGRect.zero)
        addSubview(titleLabel)
        addSubview(arrowImg)
        addSubview(clearBtn)
        
        let redImg0 = UIImageView(image: UIImage(named:"sign_picture"))
        addSubview(redImg0)
        redImg0.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(13)
            make.width.height.equalTo(13)
        }
        
        titleLabel.text = title
        titleLabel.font = UIFont(name: "PingFang SC", size: 15)
        titleLabel.textColor = UIColor(hexString:"#040000")
        arrowImg.image = UIImage(named:"return")
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(redImg0.snp.right).offset(10)
            make.centerY.equalToSuperview()
        }
        arrowImg.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-10)
            make.centerY.equalToSuperview()
            make.height.equalTo(titleLabel)
        }
        clearBtn.backgroundColor = UIColor.clear
        clearBtn.addTarget(onView, action: onClick, for: .touchUpInside)
        clearBtn.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

protocol DetailViewDelegate {
    func receiveBtnClick()
    func commitBtnClick()
    func redoBtnClick()
    func archieveBtnClick()
    func subTask()
    func taskMember()
    func taskFile()
    func communicaBtnClick()
}


